package com.skyolder.milkbee.service;

import java.time.ZonedDateTime;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skyolder.milkbee.domain.Gift;
import com.skyolder.milkbee.domain.Leaguer;
import com.skyolder.milkbee.domain.Present;
import com.skyolder.milkbee.repository.GiftRepository;
import com.skyolder.milkbee.repository.LeaguerRepository;
import com.skyolder.milkbee.repository.PresentRepository;

/**
 * Service Implementation for managing Gift.
 */
@Service
@Transactional
public class GiftService {

    private final Logger log = LoggerFactory.getLogger(GiftService.class);
    
    @Inject
    private GiftRepository giftRepository;
    @Inject
    private LeaguerRepository leaguerRepository;
    @Inject
    private PresentRepository presentRepository;

    /**
     * Save a gift.
     *
     * @param gift the entity to save
     * @return the persisted entity
     */
    public Gift save(Gift gift) {
        log.debug("Request to save Gift : {}", gift);
        Gift result = giftRepository.save(gift);
        return result;
    }

    /**
     *  Get all the gifts.
     *  
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public Page<Gift> findAll(Pageable pageable) {
        log.debug("Request to get all Gifts");
        Page<Gift> result = giftRepository.findAll(pageable);
        return result;
    }

    /**
     *  Get one gift by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public Gift findOne(Long id) {
        log.debug("Request to get Gift : {}", id);
        Gift gift = giftRepository.findOne(id);
        return gift;
    }

    /**
     *  Delete the  gift by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Gift : {}", id);
        giftRepository.delete(id);
    }
    
    public boolean sendGift( Gift  gift,Leaguer senderLeaguer,Leaguer reciverleaguer){
    	Long cost=gift.getGold();
    	long gold=senderLeaguer.getGold();
    	if(cost>gold){
    		return false;
    	}
    	senderLeaguer.setGold(senderLeaguer.getGold()-cost);
    	leaguerRepository.save(senderLeaguer);
    	Present present=new Present();
    	present.setCost(cost.intValue());
    	present.setCreatedAt(ZonedDateTime.now());
    	present.setGiftId(gift.getId());
    	present.setReason("");
    	present.setRecieverId(reciverleaguer.getUserId());
    	present.setRecieverLeaguerId(reciverleaguer.getId());
    	present.setSenderId(senderLeaguer.getUserId());
    	present.setSenderLeaguerId(senderLeaguer.getId());
    	present.setTitle(gift.getTitle());
    	presentRepository.save(present);
    	return true;
    }
}
